package com.net.xpay.backend.scheduler;


import com.net.xpay.common.domain.notify.ChargeRecordNotifyRecord;
import com.net.xpay.common.manager.notify.ChargeRecordNotifyRecordManager;
import com.net.xpay.core.service.ChargeRecordNotifyRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 2019/7/15 16:28

 * 充值服务费回调通知
 */
@Slf4j
@Component
public class ChargeRecordNotifyRecordScheduler {

    @Autowired
    private ChargeRecordNotifyRecordManager chargeRecordNotifyRecordManager;

    @Autowired
    private ChargeRecordNotifyRecordService chargeRecordNotifyRecordService;

    /**
     * 清理过期的数据
     */
    @Scheduled(cron = "0 0 2 * * *")
    public void cleanChargeRecordNotifyRecord() {
        log.info("cleanChargeRecordNotifyRecord start");

        //只保留一个月
        LocalDateTime deadline = LocalDateTime.now().minusMonths(1L);
        chargeRecordNotifyRecordManager.deleteFinishedRecord(deadline);

        log.info("cleanChargeRecordNotifyRecord end");
    }

    /**
     * 回调通知
     */
    @Scheduled(cron = "*/10 * * * * *")
    public void notifyChargeRecordNotifyRecord() {
        List<Long> ids = chargeRecordNotifyRecordManager.listWaitNotifyRecordIds(20L);
        for (Long id : ids) {
            try {
                ChargeRecordNotifyRecord chargeRecordNotifyRecord = chargeRecordNotifyRecordManager.getById(id);
                chargeRecordNotifyRecordService.notifyChargeRecordNotifyRecord(chargeRecordNotifyRecord);
            } catch (Exception e) {
                log.error("notifyChargeRecordNotifyRecord error|id={}", id, e);
            }

        }
    }




}
